<template>
  <el-card class="main-card">
    <com-area title="查询条件">
      <template v-slot:main>
        <com-search
          ref="neuSearch"
          :config="searchParams.config"
          :formData="searchParams.formData"
          :buttons="searchParams.button"
          :searchLayoutParam="searchParams.searchLayoutParam"
        />
      </template>
    </com-area>
    <com-area
      title="医生列表"
      :loading="loading"
      :loading-size="1"
      :auto="true"
    >
      <template v-slot:toolbar>
        <com-toolBar :btnList="toolBarParams.btnList" />
      </template>
      <template v-slot:main>
        <com-table
          ref="table"
          :data="tableParams.tableData"
          :options="tableParams.options"
          :export="null"
          @search="search"
        />
      </template>
    </com-area>
    <!-- 菜单对话框 -->
    <el-dialog :visible.sync="addOrEditModal" width="30%">
      <div slot="title" class="dialog-title-container"  ref="doctorTitle" />
      <el-form label-width="80px" size="medium" :model="doctorForm">
        <el-form-item label="姓名">
          <el-input v-model="doctorForm.name" style="width:250px" />
        </el-form-item>
        <el-form-item label="医院">
          <el-input v-model="doctorForm.medical" style="width:250px" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="doctorForm.phone" style="width:250px" />
        </el-form-item>
        <el-form-item label="科室">
          <el-input v-model="doctorForm.department" style="width:250px" />
        </el-form-item>
        <el-form-item label="专业">
          <el-input v-model="doctorForm.specialty" style="width:250px" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addOrEditModal = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdateDoctor">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { getDoctors, listDoctors, addDoctors, updateDoctors } from '@/api/business/doctors'
import searchParams from "./SearchParams";
import tableParams from "./TableParams";
import toolBarParams from "./ToolBarParams";

export default {
  created() {
    this.search();
  },
  data: function() {
    return {
      searchParams: searchParams(this),
      tableParams: tableParams(this),
      toolBarParams: toolBarParams(this),
      loading: true,
      keywords: null,
      currentPage: 1,
      pageSize: 10,
      addOrEditModal: false,
      addOrEditType: "",
      doctorForm: {
        name: "",
        medical: "",
        phone: "",
        department: "",
        email: "",
        specialty: ""
      }
    };
  },
  methods: {
    search(reqParams) {
      console.log(reqParams);
      this.loading = true;
      listDoctors({
          page: reqParams ? reqParams.currentPage : this.currentPage,
          limit: reqParams ? reqParams.pageSize : this.pageSize,
          name: reqParams ? reqParams.name : "",
          medical: reqParams ? reqParams.medical : "",
          phone: reqParams ? reqParams.phone : "",
          department: reqParams ? reqParams.department : "",
          specialty: reqParams ? reqParams.specialty : ""
        })
        .then(({ data }) => {
          this.tableParams.options.currentPage = reqParams ? reqParams.currentPage : this.currentPage;
          this.tableParams.options.pageSize = reqParams ? reqParams.pageSize : this.pageSize;
          this.tableParams.options.total = data.total;
          this.tableParams.tableData = data.list;
          this.loading = false;
        });
    },
    openAddOrEditModel(row, type) {
      this.addOrEditType = type;
      console.log(row);
      this.$refs.doctorTitle.innerHTML = row ? "修改" : "新增";
      if (row != null) {
        this.doctorForm = JSON.parse(JSON.stringify(row));
      } else {
        this.doctorForm = {
          name: "",
          medical: "",
          phone: "",
          department: "",
          email: "",
          specialty: ""
        };
      }
      this.addOrEditModal = true;
    },
    async saveOrUpdateDoctor() {
      if (this.doctorForm.name === "") {
        this.$message.error("医生姓名不能为空");
        return false;
      }
      let type = this.addOrEditType;
      let result ;
      if(type === "add"){
          await addDoctors(this.doctorForm).then(data => {
          result = data;
        });
      }else {
        await updateDoctors(this.doctorForm).then(data => {
          result = data;
        });
      }
      if (result.isSuccess) {
        this.$notify.success({
          title: "成功",
          message: result.message
        });
        this.search();
      } else {
        this.$notify.error({
          title: "失败",
          message: result.message
        });
      }
      this.addOrEditModal = false;
    }
  }
};
</script>
<style scoped></style>
